import { findFirstRankNavigation, findNavigationByFirstRank } from '../../api/classify'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    activeKey: 0,
    listMain: [],
    tabActive: '',
    rank: 0
  },

  onLoad: function (options) {
    this.init();
  },

  init() {
    findFirstRankNavigation().then(res => {
      this.setData({
        list: res,
        activeKey: 0
      })
      this.getSubClassify(0)
    })
  },

  onClickNav({detail = {}}) {
    if (this.data.activeId !== detail) {
      this.setData({
        activeId: detail
      });
      this.getSubClassify(detail)
    }
  },

  getSubClassify(num) {
    const _this = this
    this.setData({
      rank: 0
    })
    findNavigationByFirstRank({pNavId: _this.data.list[num].navId}).then(res => {
      this.setData({
        listMain: res,
        rank: res[0].rank,
        tabActive: res[0].rank==2?res[0].navId:''
      })
      console.log(this.data.tabActive)
    })
  },

  setTabActive(e) {
    this.setData({
      tabActive: e.detail.name
    })
    console.log(this.data.tabActive)
  },

  toPageMessage() {
    wx.navigateTo({ url: '/packageA/pages/message/message' })
  },

  navigateTo(e) {
    const data = e.currentTarget.dataset;
    wx.navigateTo({
      url: '/packageA/pages/searchResult/searchResult?keyword=' + data.tag + '&navTagsId=' + data.navid
    })
  },

  reset() {
    this.setData({
      list: [],
      activeKey: 0,
      listMain: [],
      tabActive: '',
      rank: 0
    })
    this.init()
  }
})
